package com.anolesoft.yr.store.stk.ctrl;

import cn.hutool.core.util.StrUtil;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.core.web.page.TableDataInfo;
import com.anolesoft.system.fac.entity.Employee;
import com.anolesoft.system.fac.service.IEmployeeService;
import com.anolesoft.yr.mbus.cont.entity.ContType;
import com.anolesoft.yr.mbus.cont.entity.ContTypeExample;
import com.anolesoft.yr.mbus.cont.entity.ContVariable;
import com.anolesoft.yr.mbus.cont.entity.ContVariableExample;
import com.anolesoft.yr.store.stk.entity.StkType;
import com.anolesoft.yr.store.stk.entity.StkTypeExample;
import com.anolesoft.yr.store.stk.service.StkTypeService;
import com.anolesoft.yr.store.stk.util.StringUtil;
import com.anolesoft.yr.util.SecurityUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Archie
 */
@AllArgsConstructor
@RestController()
@RequestMapping("/stk/stk_type")
public class StkTypeController {
    private StkTypeService stkTypeService;
    private IEmployeeService iEmployeeService;
    @Transactional
    @RequestMapping(value = "/get", produces = {"application/json;charset=UTF-8"})
    public AjaxResult get(StkType stkType){
        Page<StkType> page = new Page<>(stkType.getPage(), stkType.getRows());
        String stkName = StrUtil.nullToEmpty(stkType.getStkName());
        LambdaQueryWrapper<StkType> like = new QueryWrapper<StkType>()
                .lambda()
                .like(StkType::getStkName, stkName)
                .orderByDesc(StkType::getcTime);

        Page<StkType> result = stkTypeService.page(page, like);
        LambdaQueryWrapper<Employee> emplike = new QueryWrapper<Employee>()
                .lambda();
        List<Employee> employees = iEmployeeService.getBaseMapper().selectList(emplike);
        Map<String, String> collect = employees.stream().collect(Collectors.toMap(Employee::getObjuid, Employee::getEmpName));


        List<StkType> records = result.getRecords();

        for (int i = 0; i < records.size(); i++) {
            if(records.get(i).getcUserUid()!=null){
                records.get(i).setcUserUid(collect.get(records.get(i).getcUserUid()));
            }
        }
        return AjaxResult.success(new TableDataInfo(        result.getTotal(),   records    ));
    }
    @RequestMapping(value = "/del", produces = {"application/json;charset=UTF-8"})
    public AjaxResult del(StkType stkType){

        stkTypeService.removeById(stkType.getObjuid());
        return AjaxResult.success("删除成功");
    }

    @Transactional
    @RequestMapping(value = "/delBatch", produces = {"application/json;charset=UTF-8"})
    public AjaxResult delBatch(@RequestBody List<StkType> stkTypes){
        stkTypeService.removeByIds(stkTypes.stream().map(StkType::getObjuid).collect(Collectors.toList()));
        return AjaxResult.success("删除成功");
    }
    @Transactional
    @RequestMapping(value = "/edit", produces = {"application/json;charset=UTF-8"})
    public AjaxResult edit(StkType stkType){
        String empUid = SecurityUtils.getLoginUser().getEmpUid();
        stkType.setmUserUid(empUid);
        stkType.setmTime(new Date());
        stkTypeService.updateById(stkType);
        return AjaxResult.success("修改成功");
    }
    @Transactional
    @RequestMapping(value = "/add", produces = {"application/json;charset=UTF-8"})
    public AjaxResult add(StkType stkType){
        StkTypeExample stkTypeExample = new StkTypeExample();
        stkTypeExample.createCriteria().andStkNameEqualTo(stkType.getStkName());
        List<StkType> stkTypes = stkTypeService.selectByExample(stkTypeExample);
        if (stkTypes.isEmpty()){
            String empUid = SecurityUtils.getLoginUser().getEmpUid();
            stkType.setcUserUid(empUid);
            stkType.setcTime(new Date());
            stkType.setObjuid(StringUtil.generateRundom32String());
            stkTypeService.save(stkType);
            return AjaxResult.success("保存成功");
        }else{
            return AjaxResult.success("保存失败，类型名不可以重复");
        }
    }

    @RequestMapping(value = "/unique",  produces = {"application/json;charset=UTF-8"})
    public boolean unique(String stkName,String objuid){
        objuid = StrUtil.nullToEmpty(objuid);
        StkTypeExample stkTypeExample = new StkTypeExample();
        stkTypeExample.createCriteria().andStkNameEqualTo(stkName).andObjuidNotEqualTo(objuid);
        List<StkType> stkTypes = stkTypeService.selectByExample(stkTypeExample);
        if (!stkTypes.isEmpty()) {
            return false;
        } else {
            return true;
        }
    }

    @Transactional
    @RequestMapping(value = "/getById")
    public AjaxResult getById(@RequestBody StkType stkType) {
        StkType result = stkTypeService.getById(stkType.getObjuid());
        return AjaxResult.success(result);
    }

    @RequestMapping("/getAllSelect")
    public List<StkType> getAllSelect() {
        StkTypeExample stkTypeExample = new StkTypeExample();
        List<StkType> stkTypes = stkTypeService.selectByExample(stkTypeExample);
        return stkTypes;
    }


}
